<%-- 
    Document   : viewCart
    Created on : Mar 8, 2012, 12:42:44 AM
    Author     : 013v
--%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Model.CustomerManager"%>
<%@page import="Model.ProductManager"%>

<%

    String loginAttempt = request.getParameter("loginAttempt");
    if (loginAttempt != null) {
        boolean isValid = true;

        if (loginAttempt.indexOf("<") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(">") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("\"") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("/") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(":") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(";") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("=") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("|") > -1) {
            isValid = false;
        }

        if (isValid == false) {
            loginAttempt = null;
        }
    }

    String messAttempt = request.getParameter("mess");
    boolean isValidMess = true;

    if (messAttempt != null) {
        if (messAttempt.indexOf("<") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf(">") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf("\"") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf("/") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf(":") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf(";") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf("=") > -1) {
            isValidMess = false;
        } else if (messAttempt.indexOf("|") > -1) {
            isValidMess = false;
        }

        if (isValidMess == false) {
            messAttempt = null;
        }
    }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/foo.css" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen">	
        <title>Foobar</title>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/hoverIntent.js"></script>
        <script type="text/javascript" src="js/superfish.js"></script>
        <script type="text/javascript" src="js/LoginValidation.js"></script>
        <script type="text/javascript" src="js/SearchValidation.js"></script>
        <script type="text/javascript" src="js/CartValidation.js"></script>
        <script type="text/javascript">

            // initialise plugins
            jQuery(function(){
                jQuery('ul.sf-menu').superfish();
            });
            
            function checkAuth(){
                if(<%=request.getParameter("loginAttempt")%>==false && isValid){
                    document.write('<span class="errLog">Authentication failed.</span>');
                }
                else if (<%=request.getParameter("loginMax")%>==true && isValidMax){
                    document.write('<span class="errLog">You have reached your maximum login attempts. Please try again later.</span>');
                }
            }
           

        </script>
    </head>
    <body class="bg">
        <br/>
        <div class="hmenu">
            <% if (request.getSession(true).getAttribute("username") == null) {%>
            <p style="font-size: 12px; margin-top: -15px; float: right;">If you're not yet a member, sign up <a href="signUp.jsp" class="lin">here!</a> </p>   
            <% }%>
            <img src="images/head.png" style="float: left; margin-left:20px;"/>
            <div class="log">
                <% if (request.getSession(true).getAttribute("username") != null) {%>
                <span>Hello <%=session.getAttribute("username")%>!</span><br>
                <span style="font-weight: normal; font-size: 10px;">You have <%=session.getAttribute("numCart")%> items in your cart.</span>
                <div id="nav">
                    <ul class="top">
                        <li><a href="viewCart.jsp">View Cart</a></li>
                        <li><a href="checkOut.jsp">Checkout</a></li>
                        <li><a href="viewProfile.jsp">Account Details</a></li>
                        <li><a href="LogoutServlet">Log out</a></li>
                    </ul>
                </div>
                <% } else {%>
                <span style="font-weight: bold;">Log in</span>
                <script type="text/javascript">checkAuth();</script>
                <form method="post" action="LoginServlet">
                    <label>Username:</label><input type="text" id="username" name="username"/><br/>
                    <label>Password:</label><input type="password" id="password" name="password"/><br/>
                    <input type="submit" onClick="return LoginValidation()" value="Log in" name="submit"/>
                    <label class="errLog" id="err"></label>
                </form>
                <% }%>
            </div>
            <ul class="sf-menu">
                <li class="current">
                    <a href="index.jsp">Home</a>
                </li>
                <li>
                    <a href="#">Products</a>
                    <ul>
                        <%
                            ResultSet rs = new ProductManager().getProductTypes();
                            while (rs.next()) {
                        %>
                        <li>
                            <a href="products.jsp?prd=<%=rs.getInt("ProductTypeID")%>"><%=rs.getString("ProductType")%></a>
                        </li>
                        <%}%>
                    </ul>
                </li>
                <li>
                    <a href="#">Stores</a>
                </li>
                <li>
                    <a href="#">Contact Us</a>
                </li>
                <li>
                    <form action="results.jsp">
                        <input type="text" name="search" placeholder="Enter your search"/>
                        <button type="submit" onClick="return SearchValidation(this.form);">Go</button>
                    </form>
                </li>
            </ul>
        </div>
        <div class="content">
            <%

                if (request.getParameter("mess") != null && isValidMess) {
                    if (request.getParameter("mess").equals("exnum")) {
            %>
            <span class="errMess">You are only allowed to have atleast 10 items in cart.</span>
            <%    } else if (request.getParameter("mess").equals("incart")) {%>
            <span class="errMess">Item chosen is already in cart.</span>
            <%    } else if (request.getParameter("mess").equals("outstock")) {%>
            <span class="errMess">Item is out of stock.</span>
            <%     }
                }
                if (request.getSession(true).getAttribute("username") != null && request.getSession(true).getAttribute("checkflag").equals(0)) {%>
            <br/><br/>
            <% Object cartObject = session.getAttribute("cartArray");
                String[][] userCart = (String[][]) cartObject;

                boolean cont = new CustomerManager().checkifFilled(userCart);

                if (cont == false) {
            %>
            <h3>There are no items in your cart.</h3>
            <% } else {%>
            <form method="POST" action="CustomerCartQuantity">

                <table style="margin-left: auto; margin-right: auto; border-spacing: 3px;">
                    <tr style="background-color: #BDD2FF; color: #cc5a5a;">
                        <th width="400">Product</th>
                        <th width="50">Quantity</th>
                        <th width="60">Unit Price</th>
                        <th width="100">Price</th>
                        <th width="60"></th>
                    </tr>
                    <%
                        int total = 0;
                        int index = userCart[0].length;
                        int i = 0;
                        while (i < index) {
                            if (userCart[0][i] != null) {
                    %>
                    <tr>
                        <td align="center"> <%=new ProductManager().getProdName(userCart[0][i])%> </td>
                        <td align="center"> <input style="width:65px;" type="text" name="quantity<%=i%>" value="<%=userCart[1][i]%>"/> </td>
                        <td align="right">Php  <%=new ProductManager().getProdPrice(userCart[0][i])%> </td>
                        <td align="right">
                            <% total = total + Integer.parseInt(userCart[2][i]);%>
                            Php <%=userCart[2][i]%>
                        </td>
                        <td><a href="CustomerRemoveItem?in=<%=i%>"><button>Remove</button></a> </td>
                    </tr>
                    <%    }
                            i++;
                        }%>
                    <tr>
                        <td colspan="3" align="right">Total: </td>
                        <td align="right">Php <%=total%></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="3" align="right"><input type="submit" name="opt" value="Continue Shopping"/></td>
                        <td align="left"><input type="submit" name="opt" value="Checkout"/></td>
                    </tr>
                </table>
            </form>
            <% }
            } else {%>
            <h3>You have already checked out your cart.</h3>
            <h4>Please cancel checkout to modify your shopping list.</h4>
            <% }%>
        </div>
    </body>
</html>
